<?php

namespace App\Http\Controllers\Platform;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

/**
 * 平台端用户报表
 *
 * Date: 07/05/2018
 * @author George
 * @package App\Http\Controllers\Platform
 */
class UserController extends Controller
{
	/**
	 * 人员报表
	 *
	 * Date: 07/05/2018
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function report(Request $request)
	{
		$name = $request->get('name');
		$mobile = $request->get('mobile');
		$status = $request->get('status');
		$project_id = $request->get('project_id');
		$position_id = $request->get('position_id');
		$certificate = $request->get('certificate');
		$paginate = $request->get('paginate', 15);
		$query = DB::table('company_user_pivot')->select([
			'users.id', 'users.name', 'users.certificate', 'mobile',
			'projects.name as project',
			'companies.title as company',
			'positions.name as position',
			'hiredate_at', 'departure_at'
		])->join('users', 'company_user_pivot.user_id', '=', 'users.id')
			->join('projects', 'users.project_id', '=', 'projects.id')
			->join('companies', 'company_user_pivot.company_id', '=', 'companies.id')
			->join('positions', 'users.position_id', '=', 'positions.id');

		if ($name) {
			$query->where('users.name', 'like', $name);
		}

		if ($mobile) {
			$query->where('users.mobile', $mobile);
		}

		if ($status === 'working') {
			$query->whereNotNull('departure_at');
		}

		if ($status === 'departure') {
			$query->whereNull('departure_at');
		}

		if ($project_id) {
			$query->where('project_id', $project_id);
		}

		if ($position_id) {
			$query->where('position_id', $position_id);
		}

		if ($certificate === 'licensed') {
			$query->whereNotNull('users.certificate');
		}

		if ($certificate === 'unlicensed') {
			$query->whereNull('users.certificate');
		}
		return success($query->paginate($paginate));
	}

	/**
	 * 获取人员详情
	 *
	 * Date: 07/05/2018
	 * @author George
	 * @param User $user
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function details(User $user)
	{
		return success($user);
	}
}
